<?php

class User {

    function User() {
    }
    public function execute() {
    	global $page;
    	$this->getUserList($page);
    	$page->success = true;
    }
    
    
    public function getUserList($page) {
    	$where = "";
    	if($_POST[qq]&&$_POST[qq]!="") {
    		$where = $where." AND a.qq like '{$_POST[qq]}%'";
    	}
    	$sql = "SELECT a.*,c.role_name from td_normal_user_info a ".
    		"LEFT JOIN td_role_user_id b ON a.id = b.user_id ".
    		"LEFT JOIN td_role c on b.role_id = c.id where 1=1 {$where} order by  a.favorable desc,a.qq desc limit {$_REQUEST[start]},{$_REQUEST[limit]}" ;
    	$sqlC =  "SELECT count(DISTINCT a.qq) cou from td_normal_user_info a ".
    		"LEFT JOIN td_role_user_id b ON a.id = b.user_id ".
    		"LEFT JOIN td_role c on b.role_id = c.id where 1=1 {$where}" ;
    	//$rs = DB::LimitQuery("td_normal_user_info",$cond);
    	$rs = DB::GetQueryResult($sql,false,0);
    	$cou = DB::GetQueryResult($sqlC,true,0);
    	$page->totalProperty = $cou[cou];
    	$arr = array();
    	$i=0;
    	foreach($rs as $x) {
    		
    		if($i>0 && $arr[$i-1]['qq']==$x[qq]) {
    			$arr[$i-1]['roleName'] .=",".$x[role_name];
    			continue;
    		}
    		$upt = substr($x[update_time],5);
    		$tem = array(
    			'id'=>$x[id],
    			'qq'=>$x[qq],
    			'nick'=>$x[nick],
    			'isFriend' =>$x[is_friend],
    			'favorable'=>$x[favorable],
    			'inviteTime'=>$x[invite_time],
    			'updateTime'=>$upt,
    			'isBlacklist'=>$x[is_blacklist],
    			'studyTime'=>$x[study_time],
    			'firstStudy'=>$x[first_study],
    			'roleName'=>$x[role_name]
    		);
    		$arr[$i++] = $tem;
    	}
    	$page->root = $arr;
    }
    public function updateFiled() {
    	global $page;
    	$field = $_POST['fieldName'];
    	$newValue = $_POST['value'];
    	$id = $_POST['id'];
    	if($field=='roleIds') {
    		$newValue = explode(",",$newValue);
    		//echo $newValue;
    		for($i=0;$i<count($newValue);$i++) {
    			$arr = array(
    				'role_id'=>$newValue[$i],
    				'user_id'=>$id
    			);
    			DB::SaveTableRow('td_role_user_id',$arr);
    		}
    		$page->success = true ;
    		return;
    	}
    	$sql = "update td_normal_user_info set `{$field}` = '{$newValue}' where id ={$id} ";
    	if(DB::Query($sql)) {
    		$page->success = true ;
    	} else {
    		$page->success = false ;
    	}
    }
}
?>